﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SqlToLua
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        List<string> checkedTables = new List<string>();
        int index = 0;

        string codePath = "E:\\CreateData\\";
        string dataPath = "E:\\CreateData\\";
        string codeFileName = "codepath.txt";
        string dataFileName = "datapath.txt";

        SQlReader sqlReader;

        private void MySqlToByte_Load(object sender, EventArgs e)
        {
            sqlReader = new SQlReader(CreateNext);
            List<string> tableList = sqlReader.GetAllTableName();
            for (int i = 0; i < tableList.Count; i++)
            {
                this.checkedListBox1.Items.Add(tableList[i]);
            }

            string codeFromPath = Path.Combine(Application.StartupPath, codeFileName);
            if (File.Exists(codeFromPath))
            {
                string text = System.IO.File.ReadAllText(codeFromPath, Encoding.UTF8);
                codePath = text;
            }
            string dataFromPath = Path.Combine(Application.StartupPath, dataFileName);
            if (File.Exists(dataFromPath))
            {
                string text = System.IO.File.ReadAllText(dataFromPath, Encoding.UTF8);
                dataPath = text;
            }

            this.codeTextBox.Text = codePath;

            sqlReader.SetPath(codePath, dataPath);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemCheckState(i, CheckState.Checked);
            }
        }

        private void UnSelectAllButton_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemCheckState(i, CheckState.Unchecked);
            }
        }

        private void CreateButton_Click(object sender, EventArgs e)
        {
            checkedTables.Clear();
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                if (checkedListBox1.GetItemChecked(i))
                {
                    string text = checkedListBox1.GetItemText(checkedListBox1.Items[i]);
                    checkedTables.Add(text);
                }
            }
            if (checkedTables.Count > 0)
            {
                this.Enabled = false;
                index = 0;
                CreateNext();
            }
            else
            {
                MessageBox.Show("请至少选择一个表");
            }
        }

        private delegate void CallMainThread();
        public void CreateNext()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CallMainThread(CreateNext));
            }
            else
            {
                Console.WriteLine("index = " + index);
                this.progressBar1.Value = 100 * index / checkedTables.Count;
                if (index >= checkedTables.Count)
                {
                    this.Enabled = true;
                    MessageBox.Show("全部生成完成");
                }
                else
                {
                    sqlReader.Start(checkedTables[index++]);
                }
            }
        }

        private void SaveConfig(string fileName, string text)
        {
            string fromPath = Path.Combine(Application.StartupPath, fileName);
            System.IO.File.WriteAllText(fromPath, text, Encoding.UTF8);

            sqlReader.SetPath(codePath, dataPath);
        }

        private void CodePathButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                codePath = dialog.SelectedPath;
                SaveConfig(codeFileName, codePath);
                this.codeTextBox.Text = codePath;
                //MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void DataPathButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                dataPath = dialog.SelectedPath;
                SaveConfig(dataFileName, dataPath);
                //MessageBox.Show("已选择文件夹:" + foldPath, "选择文件夹提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    } 
}
